import wifi from '@ohos.wifi';
export default {
    data: {
        title: "",
        x:150,
        y:100,
        forward_x:150,
        msg:"forward",
        forward_image:"Button_normal",
        TGA:"YZJ",
        command:"1",
        local_ip:"",
        remote_ip:"",
        speed_mode:1,
        speed:10
    },
    onInit() {
        this.title = this.$t('strings.world');
        this.UDP();
    },
    onMoveStart(e){
        console.info("开始移动"+JSON.stringify(e));
    },
    toSpeed_mode(){
        if(this.speed_mode==0)
        this.speed_mode=1;
        else if(this.speed_mode==1)
        this.speed_mode=0;

    },
    onMove(e){
        //圆心是(100,250)
        if(this.speed_mode==0){
            console.info(JSON.stringify(e))
            let nx=e.touches[0].globalY-50;
            this.x=nx;

        }
        else if(this.speed_mode==1){
            console.info(JSON.stringify(e))
            let ny=e.touches[0].globalX-50;
            this.y=ny;
            if(ny>=110){
                this.msg="trun_right"
                console.info("YZJ:正在向右转")
                this.command="3";
                this.Sync();
            }
            else if(ny<=90){
                this.msg="trun_left"
                console.info("YZJ:正在向做左转")
                this.command="4";
                this.Sync();
            }

        }

    },
    onMoveEnd(){
        this.x=150;
        this.y=100;
        this.msg="stop"
        this.command=0;
        this.Sync();

    },
    onForwardstart(e){
        this.forward_image="Button_active";
        this.forward_x=e.touches[0].globalY-50
    },
    onForward(e){
        if( e.touches[0].globalY-50<=140){
            console.info("正在前进")
            this.msg="forward"
            this.command="1"
            this.Sync();
            if(e.touches[0].globalY-50<100){
                this.forward_x=100
            }
            else
            this.forward_x=e.touches[0].globalY-50
        }
        else if(e.touches[0].globalY-50>165){
            console.info("正在后退")
            this.msg="backoff"
            this.command="2"
            this.Sync();
            if(e.touches[0].globalY-50>200){
                this.forward_x=200
            }
            else
            this.forward_x=e.touches[0].globalY-50

        }
    },
    onForwardend(){
        this.forward_x=150;
        console.info("停止前进")
        this.msg="stop"
        this.forward_image="Button_normal"
        this.command="0"
        this.Sync();

    },
    //开启UDPsocket
    UDP: async function(){
        var actionData = {};
        var action = {};
        action.bundleName = 'com.example.adsharmonyos';
        action.abilityName = "com.example.adsharmonyos.ControllerAbility";;
        action.messageCode = 1010;
        action.data = actionData;
        action.abilityType = 1;
        action.syncOption =0;
        var result = await FeatureAbility.callAbility(action);
        var ret = JSON.parse(result);
        this.local_ip = ret.local_ip
        if (ret.code == 0) {
            console.info(this.TGA+'开启UDP的结果:' + JSON.stringify(ret.abilityResult));
        } else {
            console.error(this.TGA+'UDP开启失败:' + JSON.stringify(ret.code));
        }
    },
    Sync: async function(){
        var actionData = {
        };
        actionData=this.command;
        var action = {};
        action.bundleName = 'com.example.adsharmonyos';
        action.abilityName = 'com.example.adsharmonyos.ControllerAbility';
        action.messageCode = 1007;
        action.data = actionData;
        action.abilityType = 1;
        action.syncOption = 0;

        var result = await FeatureAbility.callAbility(action);
        var ret = JSON.parse(result);
        ret = JSON.parse(result);
        console.info(this.TGA+"同步数据的结果: " + result);
        if (ret.code == 0) {
            console.info('plus result is:' + JSON.stringify(ret.abilityResult));
        } else {
            console.error('plus error code:' + JSON.stringify(ret.code));
        }
    },
    GETIP: async function(){
        var actionData = {
        };
        actionData=this.remote_ip;
        var action = {};
        action.bundleName = 'com.example.adsharmonyos';
        action.abilityName = 'com.example.adsharmonyos.ControllerAbility';
        action.messageCode = 1011;
        action.data = actionData;
        action.abilityType = 1;
        action.syncOption = 0;

        var result = await FeatureAbility.callAbility(action);
        var ret = JSON.parse(result);
        ret = JSON.parse(result);
        console.info(this.TGA+"同步IP地址的结果: " + result);
        if (ret.code == 0) {
            console.info('plus result is:' + JSON.stringify(ret.abilityResult));
        } else {
            console.error('plus error code:' + JSON.stringify(ret.code));
        }
    },
    //获取本机ip地址
    getIpAddress(){
        let ip=wifi.getIpInfo().ipAddress;
        this.local_ip = (ip >> 24 & 0xFF)+"."+ ((ip >> 16) & 0xFF)+"."+((ip >> 8) & 0xFF)+"."+(ip & 0xFF);
    },
    get_remote_ip(e){
        this.remote_ip=e.value
    }
}
